// 改版废弃搞
const rule = {
    list: {
        id: /<sup\s*no="([\s\S]+?)"/gi,
        PnameN: /topics_pnameN">([\s\S]+?)<\/span>/gi,
        imgSrc: /data-original="([\s\S]+?)"/gi,
        price: /<span class="price"[\s\S]+?>([\s\S]+?)<\/span>/gi,
        delPrice: /<del\s*class="delPrice">([\s\S]+?)<\/del>/gi,
        onSale: /<span\s*class="onSale">([\s\S]+?)<\/span>/gi,
        aHerf: /<a\s*onclick="linksa\(this\)"\s*href="([\s\S]+?)"\s*>/gi
    },
    details: {
    }
}
import cheerio from 'cheerio'
// ProductListBable 大型数据改用 cheerio

function listHtmlStringBable(rule, HtmlString) { // => Object
    try {
        let argument = Object.keys(rule),
            length = HtmlString.match(rule[argument[0]]).length, // 得到当前字符串最大能匹配到的正则数
            data = [];
        // 将正则对象最后一次查找的记录进行初始化
        for (let i = 0; i < argument.length; i++) {
            rule[argument[i]].lastIndex = 0;
        }

        for (let i = 0; i < length; i++) {
            let thisData = {}
            for (let j = 0; j < argument.length; j++) {
                let htmlBable = rule[argument[j]].exec(HtmlString)
                thisData[argument[j]] = htmlBable && htmlBable[1]
            }
            data.push(thisData)
        }
        return data
    } catch (error) {
        return null
    }
}

function detailsHtmlStringBable(rule, HtmlString) {
    let $ = cheerio.load(HtmlString);

}

let ProductListBable = function (HtmlString) {
    return listHtmlStringBable(rule.list, HtmlString)
}

let detailsBable = function (HtmlString) {
    return detailsHtmlStringBable(rule.details, HtmlString)
}


export { ProductListBable, detailsBable }